/* Copyright 2011 Elektrobit Wireless Communications Oy (EB)


  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License version 2 as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


 *All rights reserved 
 *Version:Elektrobit_Qt_1.0.0_20110305 */


#include "page.h"
#include "pageview.h"
#include <QApplication>
#include <appcommon.h>
Page::Page(QObject *parent) : QObject(parent) {
    mPageView = NULL;
}

Page::Page(const Page &page) : QObject(page.parent()) {
    mPageView = NULL;
}

int Page::setPageView(PageView *pageView) {
    this->mPageView = pageView;
}

void Page::show() {
    if(this->mPageView != NULL) {
        this->mPageView->show();
    }
}

void Page::hide() {
    if(this->mPageView != NULL) {
        this->mPageView->hide();
    }
}

bool Page::isPageViewShow() {
    if(this->mPageView != NULL) {
        if(this->mPageView->isVisible()) {
            return true;
        }
    }
    return false;
}

void Page::mouseEvent(QMouseEvent *event) {
    if(this->mPageView != NULL) {
        APPINFO << mPageView << event->pos();
        QWidget * currentWidget = mPageView->childAt(event->pos());
        if(currentWidget) {
            QWidget *parentWidget = currentWidget->parentWidget();
            QPoint pos = event->pos() - currentWidget->pos();
            while(parentWidget != NULL && parentWidget != mPageView) {
                pos = pos - parentWidget->pos();
                parentWidget = parentWidget->parentWidget();
            }
            QMouseEvent *currentEvent = new QMouseEvent(event->type(), 
                                                        pos,
                                                        event->button(), 
                                                        Qt::NoButton,
                                                        Qt::NoModifier);
            APPINFO << currentWidget << currentEvent->pos() << currentWidget->pos() << event->pos();
            QApplication::postEvent(currentWidget, currentEvent);
            delete event;
        } else {
            APPINFO << mPageView << event->pos();
            QApplication::postEvent(mPageView, event);
        }
    }
    
}
